// weblog-vue3/src/api/admin/ai.js

import axios from 'axios';

const api = axios.create({
    baseURL: '/api',
    timeout: 100000000,
    headers: {
        'Content-Type': 'application/json; charset=utf-8'
    }
});

export const chatAPI = {
    // 传统非流式API，保留以兼容现有代码
    sendMessage: async (message) => {
        try {
            const response = await api.post('/admin/chat', { message });
            return response.data;
        } catch (error) {
            console.error('API Error:', error);
            throw error;
        }
    },
    
    // 使用SSE流式连接
    createStreamConnection: (message) => {
        try {
            // 创建EventSource连接并添加重试配置
            const url = `/api/stream?message=${encodeURIComponent(message)}&t=${Date.now()}`;
            console.log(`Creating SSE connection to: ${url}`);
            
            const eventSource = new EventSource(url);
            
            // 设置默认错误处理
            eventSource.onerror = (error) => {
                console.error('EventSource default error handler:', error);
            };
            
            return eventSource;
        } catch (error) {
            console.error('Error creating EventSource:', error);
            throw error;
        }
    }
};